﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace WebApplication1.Common
{
    /// <summary>
    /// 文件操作公用
    /// </summary>
    public class FileCommon
    {

        /// <summary>
        /// C#检测文件真实类型
        /// </summary>
        /// <param name="file"></param>
        /// <returns>返回，文件真实类型</returns>
        public static string GetFileRealType(System.IO.Stream fs)
        {
            System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
            string fileclass = "";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                fileclass = buffer.ToString();
                buffer = r.ReadByte();
                fileclass += buffer.ToString();
            }
            catch
            {
                throw;
            }
            r.Close();
            fs.Close();
            /*文件扩展名说明
             *4946/104116 txt
             *7173        gif 
             *255216      jpg
             *13780       png
             *6677        bmp
             *239187      txt,aspx,asp,sql
             *208207      xls.doc.ppt
             *6063        xml
             *6033        htm,html
             *4742        js
             *8075        xlsx,zip,pptx,mmap,zip
             *8297        rar   
             *01          accdb,mdb
             *7790        exe,dll           
             *5666        psd 
             *255254      rdp 
             *10056       bt种子 
             *64101       bat 
             *4059        sgf
             */
            //String[] fileType = { "255216", "7173", "6677", "13780", "8297", "5549", "870", "87111", "8075" };
            //纯图片
            Dictionary<string, string> fileTypelist = new Dictionary<string, string>();
            fileTypelist.Add("7173", "gif");
            fileTypelist.Add("255216", "jpg");
            fileTypelist.Add("13780", "png");
            fileTypelist.Add("6677", "bmp");

            string fileTypeName = "";//文件后缀名
            foreach (var item in fileTypelist)
            {
                if (item.Key.Equals(fileclass))
                {
                    fileTypeName = item.Value;//返回文件格式名
                    break;
                }
            }
            return fileTypeName;
        }


    }
}
